package edu.sju.chenshu.system.service.user.impl;

import com.github.pagehelper.PageHelper;
import edu.sju.chenshu.common.core.domain.Result;
import edu.sju.chenshu.common.core.domain.TableDataInfo;
import edu.sju.chenshu.common.core.enums.ResultCode;
import edu.sju.chenshu.common.core.service.BaseService;
import edu.sju.chenshu.common.security.exception.ServiceException;
import edu.sju.chenshu.system.domain.question.dto.UserDTO;
import edu.sju.chenshu.system.domain.user.User;
import edu.sju.chenshu.system.domain.user.dto.UserQueryDTO;
import edu.sju.chenshu.system.domain.user.vo.UserVO;
import edu.sju.chenshu.system.mapper.user.UserMapper;
import edu.sju.chenshu.system.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService, BaseService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public TableDataInfo list(UserQueryDTO userDTO) {
        PageHelper.startPage(userDTO.getPageNum(), userDTO.getPageSize());
        List<UserVO> userVoList = userMapper.selectUserList(userDTO);
        return getTableDataInfo(userVoList);
    }

    @Override
    public Result<Void> updateStatus(UserDTO userDTO) {
        User user = userMapper.selectById(userDTO.getUserId());
        if (user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        user.setStatus(userDTO.getStatus());
        return toResult(userMapper.updateById(user));
    }
}
